package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzUsersyncMapper;
import com.ruoyi.system.domain.DzUsersync;
import com.ruoyi.system.service.IDzUsersyncService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 同步用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-31
 */
@Service
public class DzUsersyncServiceImpl implements IDzUsersyncService 
{
    @Autowired
    private DzUsersyncMapper dzUsersyncMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询同步用户
     * 
     * @param yhId 同步用户主键
     * @return 同步用户
     */
    @Override
    public DzUsersync selectDzUsersyncByYhId(Long yhId)
    {
        return dzUsersyncMapper.selectDzUsersyncByYhId(yhId);
    }

    /**
     * 查询同步用户列表
     * 
     * @param dzUsersync 同步用户
     * @return 同步用户
     */
    @Override
    public List<DzUsersync> selectDzUsersyncList(DzUsersync dzUsersync)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzUsersync.setDatascope(datascope);
        if(datascope==4){
            dzUsersync.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzUsersync.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzUsersync.setCheckHuiyuanId(0l);
            }

        }
        return dzUsersyncMapper.selectDzUsersyncList(dzUsersync);
    }

    @Override
    public List<DzUsersync> selectDzUsersyncListWeb(DzUsersync dzUsersync)
    {
        return dzUsersyncMapper.selectDzUsersyncList(dzUsersync);
    }

    /**
     * 新增同步用户
     * 
     * @param dzUsersync 同步用户
     * @return 结果
     */
    @Override
    public int insertDzUsersync(DzUsersync dzUsersync)
    {
        return dzUsersyncMapper.insertDzUsersync(dzUsersync);
    }

    /**
     * 修改同步用户
     * 
     * @param dzUsersync 同步用户
     * @return 结果
     */
    @Override
    public int updateDzUsersync(DzUsersync dzUsersync)
    {
        return dzUsersyncMapper.updateDzUsersync(dzUsersync);
    }

    /**
     * 批量删除同步用户
     * 
     * @param yhIds 需要删除的同步用户主键
     * @return 结果
     */
    @Override
    public int deleteDzUsersyncByYhIds(Long[] yhIds)
    {
        return dzUsersyncMapper.deleteDzUsersyncByYhIds(yhIds);
    }

    /**
     * 删除同步用户信息
     * 
     * @param yhId 同步用户主键
     * @return 结果
     */
    @Override
    public int deleteDzUsersyncByYhId(Long yhId)
    {
        return dzUsersyncMapper.deleteDzUsersyncByYhId(yhId);
    }
}
